class Solution(object):
    def sortedSquares(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        i = 0
        j = len(nums) - 1
        n = len(nums) - 1
        # 定义列表
        res = [float('inf')] * len(nums)

        while i <= j:
            if nums[i] ** 2 < nums[j] ** 2:
                res[n] = nums[j] ** 2
                j -= 1
            else:
                res[n] = nums[i] ** 2
                i += 1

            n -= 1

        return res
        # i = 0
        # j = len(nums) - 1
        # n = len(nums) - 1
        # # 定义列表
        # res = []
        #
        # while i <= j:
        #     if nums[i] ** 2 < nums[j] ** 2:
        #         res.append(nums[j] ** 2)
        #         j -= 1
        #     else:
        #         res.append(nums[i] ** 2)
        #         i += 1
        #
        #     n -= 1
        #
        # res.sort()
        #
        # return res


if __name__ == '__main__':
    nums = [-3, -1, 0, 2, 4]
    res = Solution().sortedSquares(nums)
    print(res)
